package com.tedu.day08springmvc.controller;

import com.tedu.day08springmvc.mapper.UserMapper;
import com.tedu.day08springmvc.pojo.dto.UserDto;
import com.tedu.day08springmvc.pojo.entity.User;
import com.tedu.day08springmvc.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.beans.Beans;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/users/reg")
    @ResponseBody
    public String reg(UserDto userDto){
        User user=new User();
        System.out.println(user);
        BeanUtils.copyProperties(userDto,user);
        user.setCreated(new Date());
        System.out.println(user);
        userMapper.insertUser(user);
        return "注册成功";
    }

    @RequestMapping("/users/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable int id){
        int num=userMapper.deleteUser(id);
        return num>0?"删除成功":"失败";
    }

    @RequestMapping("/users/update")
    @ResponseBody
    public String update(UserDto userDto){
        User user=new User();
        BeanUtils.copyProperties(userDto,user);
        userMapper.updateUser(user);
        return "修改成功";
    }

    //@RequestMapping(value ="/users/find" ,method = RequestMethod.GET)
    @GetMapping("/users/find")
    @ResponseBody
    public List<UserVO>find(){
        List<UserVO>userList=userMapper.selectUsers();
        return userList;
    }

    public UserVO findById(){
        return null;
    }

}
